package com.bw.a2207a.http

import com.blankj.utilcode.util.SPUtils
import com.bw.a2207a.Const.Companion.URL_BASE
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.util.concurrent.TimeUnit

/**
 *
 * @ClassName: HttpUtil
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/10/16 11:58 AM
 * @Version: 1.0
 */
object HttpUtil {
    val okHttpClient = OkHttpClient()
        .newBuilder()
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .addInterceptor(Interceptor { chain ->
            val request = chain.request().newBuilder()
                .addHeader("token", SPUtils.getInstance().getString("token"))
                .build()
             chain.proceed(request)
        })
//        .addInterceptor(object :Interceptor{
//            override fun intercept(chain: Interceptor.Chain): Response {
//                val request = chain.request().newBuilder()
//                    .addHeader("token", SPUtils.getInstance().getString("token"))
//                    .build()
//                return chain.proceed(request)
//            }
//        })
        .writeTimeout(2, TimeUnit.MINUTES)//写超时
        .readTimeout(2, TimeUnit.MINUTES)//读超时
        .connectTimeout(5, TimeUnit.MINUTES)//连接超时
        .retryOnConnectionFailure(true)
        .build()

    val retrofit = Retrofit
        .Builder()
        .baseUrl(URL_BASE)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()


    //调用retrofit的扩展函数，构造retrofit 对象
    val apiService = retrofit.create<ApiService>()

    //调用retrofit的函数，构造retrofit 对象
    val apiService1 = retrofit.create(ApiService::class.java)

}